'''
Created on 21/10/2012

@author: Barrios - Correa - Felippone
'''

from Parser import yacc

class Console:
    
    def commandLine(self):
        print 'Metalangue 1.0 <r1:1, Oct 23 2012, 17:55>. SHALOSH team'
        print 'Type \"credits\" from more information.'
        print 'Type \"exit\" to exit the command.'
        print 'Type \"clear\" to clean the code wrote above.'
        print 'Type \"showcode\" to see the code wrote above.'
        print 'Confirm the source code leaving the line empty.'
        code = ''
        while 1:
            try:
                line = raw_input('mtg > ')
            except EOFError:                
                break
            if line == 'credits' and code=='':
                print 'Barrios, Florencia\nCorrea, Mauro\nFelippone,Jorge'
            else:
                if line == 'exit' and code=='':
                    break
                else:
                    if line == 'clear':
                        print 'Deleting source code.'
                        code = ''
                        print 'The source code has been eliminated.'
                    else:
                        if line == 'showcode':
                            print '*****Loading source code...****'
                            print 'Source code wrote:'
                            print code
                        else:                            
                            if line <> '':
                                code += line
                                code += '\n'
                                print 'Line added to source code'
                            else:
                                try:
                                    if code == '':
                                        print 'Source code can not be empty.'
                                    else:
                                        print 'Code:\n' + code +'\n*****Generating AST*****'
                                        ast = yacc.parse(code)
                                        if ast:
                                            print '\n*****Building and parsing*****'
                                            print ast.build_parser()
                                except Exception as e:
                                    print 'Se genero un error al procesar el codigo fuente:' + str(e)
                                code = ''
        print 'Bye.\n'
        
c = Console()
c.commandLine()
            
    